{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b461c7cd-c5e7-4055-adf6-166d8843b87f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:38:17.908772Z",
     "iopub.status.busy": "2024-09-29T13:38:17.908212Z",
     "iopub.status.idle": "2024-09-29T13:38:17.917147Z",
     "shell.execute_reply": "2024-09-29T13:38:17.915362Z",
     "shell.execute_reply.started": "2024-09-29T13:38:17.908718Z"
    }
   },
   "outputs": [],
   "source": [
    "# !pip install -U langchain\n",
    "# !pip install -U langchain-openai\n",
    "# !pip install -U langchain_core"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b8dc095-c6d2-4c27-b418-568039e226d1",
   "metadata": {},
   "source": [
    "- https://python.langchain.com/v0.1/docs/modules/agents/how_to/custom_agent/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "856c5727-4d3c-4e60-a3c0-87d049a8124a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:00:27.865802Z",
     "iopub.status.busy": "2024-09-29T14:00:27.865406Z",
     "iopub.status.idle": "2024-09-29T14:00:27.873899Z",
     "shell.execute_reply": "2024-09-29T14:00:27.872580Z",
     "shell.execute_reply.started": "2024-09-29T14:00:27.865770Z"
    }
   },
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from IPython.display import Image\n",
    "assert load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "be9f35db-a12a-4c4d-a68e-87527d14a692",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:01:29.656098Z",
     "iopub.status.busy": "2024-09-29T14:01:29.655479Z",
     "iopub.status.idle": "2024-09-29T14:01:29.665661Z",
     "shell.execute_reply": "2024-09-29T14:01:29.663457Z",
     "shell.execute_reply.started": "2024-09-29T14:01:29.656046Z"
    }
   },
   "outputs": [],
   "source": [
    "os.environ['LANGCHAIN_PROJECT'] = '0929'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8dbac817-b387-40db-8c2c-73561a0b7d7e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:38:59.119156Z",
     "iopub.status.busy": "2024-09-29T13:38:59.118857Z",
     "iopub.status.idle": "2024-09-29T13:38:59.220886Z",
     "shell.execute_reply": "2024-09-29T13:38:59.220393Z",
     "shell.execute_reply.started": "2024-09-29T13:38:59.119134Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# load llm\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
    "\n",
    "# tools\n",
    "from langchain.agents import tool\n",
    "@tool\n",
    "def get_word_length(word: str) -> int:\n",
    "    \"\"\"Returns the length of a word.\"\"\"\n",
    "    return len(word)\n",
    "    \n",
    "print(get_word_length.invoke(\"abc\"))\n",
    "\n",
    "tools = [get_word_length]\n",
    "llm_with_tools = llm.bind_tools(tools)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e2963c3-5007-4d1f-b129-66605794661b",
   "metadata": {},
   "source": [
    "### prompt template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "49555f26-eedf-4bef-a4ce-73677ea4ba7c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:40:36.210336Z",
     "iopub.status.busy": "2024-09-29T13:40:36.209703Z",
     "iopub.status.idle": "2024-09-29T13:40:36.218462Z",
     "shell.execute_reply": "2024-09-29T13:40:36.216699Z",
     "shell.execute_reply.started": "2024-09-29T13:40:36.210281Z"
    }
   },
   "outputs": [],
   "source": [
    "from rich.pretty import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "334dced1-0ef6-483a-b1b0-0a043f73fec9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:40:39.303658Z",
     "iopub.status.busy": "2024-09-29T13:40:39.303254Z",
     "iopub.status.idle": "2024-09-29T13:40:39.339920Z",
     "shell.execute_reply": "2024-09-29T13:40:39.337939Z",
     "shell.execute_reply.started": "2024-09-29T13:40:39.303625Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">ChatPromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_variables</span>=<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'agent_scratchpad'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'input'</span><span style=\"font-weight: bold\">]</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_types</span>=<span style=\"font-weight: bold\">{</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #008000; text-decoration-color: #008000\">'agent_scratchpad'</span>: list<span style=\"font-weight: bold\">[</span>typing.Annotated<span style=\"font-weight: bold\">[</span>typing.Union<span style=\"font-weight: bold\">[</span>typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.ai.AIMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'ai'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.human.HumanMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'human'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.chat.ChatMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'chat'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.system.SystemMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'system'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.function.FunctionMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'function'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.tool.ToolMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'tool'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.ai.AIMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'AIMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.human.HumanMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'HumanMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.chat.ChatMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'ChatMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.system.SystemMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'SystemMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.function.FunctionMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'FunctionMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.tool.ToolMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'ToolMessageChunk'</span><span style=\"font-weight: bold\">)]]</span>, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">FieldInfo</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">annotation</span>=<span style=\"color: #800080; text-decoration-color: #800080\">NoneType</span>, <span style=\"color: #808000; text-decoration-color: #808000\">required</span>=<span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>, <span style=\"color: #808000; text-decoration-color: #808000\">discriminator</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Discriminator</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">discriminator</span>=<span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">function</span><span style=\"color: #000000; text-decoration-color: #000000\"> _get_type at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7e42c61ae700</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #808000; text-decoration-color: #808000\">custom_error_type</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>, <span style=\"color: #808000; text-decoration-color: #808000\">custom_error_message</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>, <span style=\"color: #808000; text-decoration-color: #808000\">custom_error_context</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span><span style=\"font-weight: bold\">))]]</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"font-weight: bold\">}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">partial_variables</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">messages</span>=<span style=\"font-weight: bold\">[</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">SystemMessagePromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">prompt</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">PromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_variables</span>=<span style=\"font-weight: bold\">[]</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_types</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">partial_variables</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">template</span>=<span style=\"color: #008000; text-decoration-color: #008000\">\"You are very powerful assistant, but don't know current events\"</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">additional_kwargs</span>=<span style=\"font-weight: bold\">{}</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">HumanMessagePromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">prompt</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">PromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_variables</span>=<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'input'</span><span style=\"font-weight: bold\">]</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_types</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">partial_variables</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">template</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'{input}'</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">additional_kwargs</span>=<span style=\"font-weight: bold\">{}</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">MessagesPlaceholder</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">variable_name</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'agent_scratchpad'</span><span style=\"font-weight: bold\">)</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"font-weight: bold\">]</span>\n",
       "<span style=\"font-weight: bold\">)</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;35mChatPromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33minput_variables\u001b[0m=\u001b[1m[\u001b[0m\u001b[32m'agent_scratchpad'\u001b[0m, \u001b[32m'input'\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33minput_types\u001b[0m=\u001b[1m{\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[32m'agent_scratchpad'\u001b[0m: list\u001b[1m[\u001b[0mtyping.Annotated\u001b[1m[\u001b[0mtyping.Union\u001b[1m[\u001b[0mtyping.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.ai.AIMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'ai'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.human.HumanMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'human'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.chat.ChatMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'chat'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.system.SystemMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'system'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.function.FunctionMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'function'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.tool.ToolMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'tool'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.ai.AIMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'AIMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.human.HumanMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'HumanMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.chat.ChatMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'ChatMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.system.SystemMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'SystemMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.function.FunctionMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'FunctionMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.tool.ToolMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'ToolMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m, \u001b[1;35mFieldInfo\u001b[0m\u001b[1m(\u001b[0m\u001b[33mannotation\u001b[0m=\u001b[35mNoneType\u001b[0m, \u001b[33mrequired\u001b[0m=\u001b[3;92mTrue\u001b[0m, \u001b[33mdiscriminator\u001b[0m=\u001b[1;35mDiscriminator\u001b[0m\u001b[1m(\u001b[0m\u001b[33mdiscriminator\u001b[0m=\u001b[1m<\u001b[0m\u001b[1;95mfunction\u001b[0m\u001b[39m _get_type at \u001b[0m\u001b[1;36m0x7e42c61ae700\u001b[0m\u001b[1m>\u001b[0m, \u001b[33mcustom_error_type\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[33mcustom_error_message\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[33mcustom_error_context\u001b[0m=\u001b[3;35mNone\u001b[0m\u001b[1m)\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\n",
       "\u001b[2;32m│   \u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33mpartial_variables\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33mmessages\u001b[0m=\u001b[1m[\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mSystemMessagePromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33mprompt\u001b[0m=\u001b[1;35mPromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_variables\u001b[0m=\u001b[1m[\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_types\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mpartial_variables\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mtemplate\u001b[0m=\u001b[32m\"You\u001b[0m\u001b[32m are very powerful assistant, but don't know current events\"\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33madditional_kwargs\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mHumanMessagePromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33mprompt\u001b[0m=\u001b[1;35mPromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_variables\u001b[0m=\u001b[1m[\u001b[0m\u001b[32m'input'\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_types\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mpartial_variables\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mtemplate\u001b[0m=\u001b[32m'\u001b[0m\u001b[32m{\u001b[0m\u001b[32minput\u001b[0m\u001b[32m}\u001b[0m\u001b[32m'\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33madditional_kwargs\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mMessagesPlaceholder\u001b[0m\u001b[1m(\u001b[0m\u001b[33mvariable_name\u001b[0m=\u001b[32m'agent_scratchpad'\u001b[0m\u001b[1m)\u001b[0m\n",
       "\u001b[2;32m│   \u001b[0m\u001b[1m]\u001b[0m\n",
       "\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"You are very powerful assistant, but don't know current events\",\n",
    "        ),\n",
    "        (\"user\", \"{input}\"),\n",
    "        MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "pprint(prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52cb6539-1642-43ba-bae7-7b7960f2a509",
   "metadata": {},
   "source": [
    "### agent & agent executor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fa187ba0-2e3c-48d6-8d3e-c86df9f26a9c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:53:51.945922Z",
     "iopub.status.busy": "2024-09-29T13:53:51.945619Z",
     "iopub.status.idle": "2024-09-29T13:53:51.954959Z",
     "shell.execute_reply": "2024-09-29T13:53:51.953249Z",
     "shell.execute_reply.started": "2024-09-29T13:53:51.945900Z"
    }
   },
   "outputs": [],
   "source": [
    "from langchain.agents.format_scratchpad.openai_tools import (\n",
    "    format_to_openai_tool_messages,\n",
    ")\n",
    "from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser\n",
    "\n",
    "agent = (\n",
    "    {\n",
    "        \"input\": lambda x: x[\"input\"],\n",
    "        \"agent_scratchpad\": lambda x: format_to_openai_tool_messages(x[\"intermediate_steps\"]),\n",
    "    }\n",
    "    | prompt\n",
    "    | llm_with_tools\n",
    "    | OpenAIToolsAgentOutputParser()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2618e5db-71be-4781-8c08-035d91f02fd4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:58:25.910557Z",
     "iopub.status.busy": "2024-09-29T13:58:25.909931Z",
     "iopub.status.idle": "2024-09-29T13:58:25.922364Z",
     "shell.execute_reply": "2024-09-29T13:58:25.920642Z",
     "shell.execute_reply.started": "2024-09-29T13:58:25.910504Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"https://miro.medium.com/v2/resize:fit:4800/format:webp/1*CD2Svi5BUZaG-d-KWeU3ug.png\" width=\"500\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image(url='https://miro.medium.com/v2/resize:fit:4800/format:webp/1*CD2Svi5BUZaG-d-KWeU3ug.png', width=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8c46d86d-03ec-4aee-8505-59c106ad1381",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T13:53:55.572861Z",
     "iopub.status.busy": "2024-09-29T13:53:55.572251Z",
     "iopub.status.idle": "2024-09-29T13:53:55.581591Z",
     "shell.execute_reply": "2024-09-29T13:53:55.579830Z",
     "shell.execute_reply.started": "2024-09-29T13:53:55.572808Z"
    }
   },
   "outputs": [],
   "source": [
    "from langchain.agents import AgentExecutor\n",
    "\n",
    "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f6ce019-0610-4b20-b739-9848f7bbdf09",
   "metadata": {},
   "source": [
    "- `intermediate steps`: Denotes preceding agent actions and their corresponding outputs, organized as a List of Tuples `[AgentAction, Any].`\n",
    "- The `AgentExecutor` is tasked with utilizing the Agent **until the final output is attained**. Consequently, it employs the Agent to obtain the next action, executes the returned action **iteratively**, and continues this process until a conclusive answer is generated for the given input.\n",
    "- messages list\n",
    "    - System: You are very powerful assistant, but don't know current events\n",
    "    - Human: How many letters in the word eudca\n",
    "    - AI:\n",
    "    ```\n",
    "    [\n",
    "      {\n",
    "        \"name\": \"get_word_length\",\n",
    "        \"args\": {\n",
    "          \"word\": \"eudca\"\n",
    "        },\n",
    "        \"id\": \"call_7iFwbEl7nHS3K3scMOF6ubH5\",\n",
    "        \"type\": \"tool_call\"\n",
    "      }\n",
    "    ]\n",
    "    ```\n",
    "    - Tool:\n",
    "    ```\n",
    "    5\n",
    "    ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e3612cb8-21fc-4181-b849-1cdff2310181",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:01:49.986100Z",
     "iopub.status.busy": "2024-09-29T14:01:49.985495Z",
     "iopub.status.idle": "2024-09-29T14:01:51.990903Z",
     "shell.execute_reply": "2024-09-29T14:01:51.989167Z",
     "shell.execute_reply.started": "2024-09-29T14:01:49.986048Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `get_word_length` with `{'word': 'eudca'}`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m5\u001b[0m\u001b[32;1m\u001b[1;3mThe word \"eudca\" has 5 letters.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'How many letters in the word eudca',\n",
       " 'output': 'The word \"eudca\" has 5 letters.'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke({\"input\": \"How many letters in the word eudca\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "669cb06a-740f-4443-826e-dccc61a93a28",
   "metadata": {},
   "source": [
    "### with `chat_history`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f43d66f2-abd0-429c-95ed-f7e21fcbc442",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:09:46.881450Z",
     "iopub.status.busy": "2024-09-29T14:09:46.880815Z",
     "iopub.status.idle": "2024-09-29T14:09:46.891251Z",
     "shell.execute_reply": "2024-09-29T14:09:46.889500Z",
     "shell.execute_reply.started": "2024-09-29T14:09:46.881398Z"
    }
   },
   "outputs": [],
   "source": [
    "from langchain_core.prompts import MessagesPlaceholder\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"You are very powerful assistant, but bad at calculating lengths of words.\",\n",
    "        ),\n",
    "        MessagesPlaceholder(variable_name='chat_history'),\n",
    "        (\"user\", \"{input}\"),\n",
    "        MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cb93427a-89e2-4fc9-9f41-4e1cd46c7f0f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:09:58.775870Z",
     "iopub.status.busy": "2024-09-29T14:09:58.775252Z",
     "iopub.status.idle": "2024-09-29T14:09:58.824659Z",
     "shell.execute_reply": "2024-09-29T14:09:58.822777Z",
     "shell.execute_reply.started": "2024-09-29T14:09:58.775816Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">ChatPromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_variables</span>=<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'agent_scratchpad'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'chat_history'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'input'</span><span style=\"font-weight: bold\">]</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_types</span>=<span style=\"font-weight: bold\">{</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #008000; text-decoration-color: #008000\">'chat_history'</span>: list<span style=\"font-weight: bold\">[</span>typing.Annotated<span style=\"font-weight: bold\">[</span>typing.Union<span style=\"font-weight: bold\">[</span>typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.ai.AIMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'ai'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.human.HumanMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'human'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.chat.ChatMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'chat'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.system.SystemMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'system'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.function.FunctionMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'function'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.tool.ToolMessage, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'tool'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.ai.AIMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'AIMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.human.HumanMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'HumanMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.chat.ChatMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'ChatMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.system.SystemMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'SystemMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.function.FunctionMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'FunctionMessageChunk'</span><span style=\"font-weight: bold\">)]</span>, typing.Annotated<span style=\"font-weight: bold\">[</span>langchain_core.messages.tool.ToolMessageChunk, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'ToolMessageChunk'</span><span style=\"font-weight: bold\">)]]</span>, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">FieldInfo</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">annotation</span>=<span style=\"color: #800080; text-decoration-color: #800080\">NoneType</span>, <span style=\"color: #808000; text-decoration-color: #808000\">required</span>=<span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>, <span style=\"color: #808000; text-decoration-color: #808000\">discriminator</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Discriminator</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">discriminator</span>=<span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">function</span><span style=\"color: #000000; text-decoration-color: #000000\"> _get_type at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7e42c61ae700</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;, </span><span style=\"color: #808000; text-decoration-color: #808000\">custom_error_type</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span><span style=\"color: #000000; text-decoration-color: #000000\">, </span><span style=\"color: #808000; text-decoration-color: #808000\">custom_error_message</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span><span style=\"color: #000000; text-decoration-color: #000000\">, </span><span style=\"color: #808000; text-decoration-color: #808000\">custom_error_context</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">))]]</span><span style=\"color: #000000; text-decoration-color: #000000\">,</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #008000; text-decoration-color: #008000\">'agent_scratchpad'</span><span style=\"color: #000000; text-decoration-color: #000000\">: list</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">typing.Union</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.ai.AIMessage, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'ai'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.human.HumanMessage, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'human'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.chat.ChatMessage, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'chat'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.system.SystemMessage, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'system'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.function.FunctionMessage, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'function'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.tool.ToolMessage, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'tool'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.ai.AIMessageChunk, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'AIMessageChunk'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.human.HumanMessageChunk, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'HumanMessageChunk'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.chat.ChatMessageChunk, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'ChatMessageChunk'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.system.SystemMessageChunk, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'SystemMessageChunk'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.function.FunctionMessageChunk, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'FunctionMessageChunk'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]</span><span style=\"color: #000000; text-decoration-color: #000000\">, typing.Annotated</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">langchain_core.messages.tool.ToolMessageChunk, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Tag</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">tag</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'ToolMessageChunk'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)]]</span><span style=\"color: #000000; text-decoration-color: #000000\">, </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">FieldInfo</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">annotation</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #800080; text-decoration-color: #800080\">NoneType</span><span style=\"color: #000000; text-decoration-color: #000000\">, </span><span style=\"color: #808000; text-decoration-color: #808000\">required</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span><span style=\"color: #000000; text-decoration-color: #000000\">, </span><span style=\"color: #808000; text-decoration-color: #808000\">discriminator</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Discriminator</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">discriminator</span><span style=\"color: #000000; text-decoration-color: #000000\">=&lt;function _get_type at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7e42c61ae700</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #808000; text-decoration-color: #808000\">custom_error_type</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>, <span style=\"color: #808000; text-decoration-color: #808000\">custom_error_message</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>, <span style=\"color: #808000; text-decoration-color: #808000\">custom_error_context</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span><span style=\"font-weight: bold\">))]]</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"font-weight: bold\">}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">partial_variables</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"color: #808000; text-decoration-color: #808000\">messages</span>=<span style=\"font-weight: bold\">[</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">SystemMessagePromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">prompt</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">PromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_variables</span>=<span style=\"font-weight: bold\">[]</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_types</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">partial_variables</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">template</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'You are very powerful assistant, but bad at calculating lengths of words.'</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">additional_kwargs</span>=<span style=\"font-weight: bold\">{}</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">MessagesPlaceholder</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">variable_name</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'chat_history'</span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">HumanMessagePromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">prompt</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">PromptTemplate</span><span style=\"font-weight: bold\">(</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_variables</span>=<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'input'</span><span style=\"font-weight: bold\">]</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">input_types</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">partial_variables</span>=<span style=\"font-weight: bold\">{}</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">template</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'{input}'</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   │   </span><span style=\"color: #808000; text-decoration-color: #808000\">additional_kwargs</span>=<span style=\"font-weight: bold\">{}</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"font-weight: bold\">)</span>,\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   │   </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">MessagesPlaceholder</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">variable_name</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'agent_scratchpad'</span><span style=\"font-weight: bold\">)</span>\n",
       "<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│   </span><span style=\"font-weight: bold\">]</span>\n",
       "<span style=\"font-weight: bold\">)</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;35mChatPromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33minput_variables\u001b[0m=\u001b[1m[\u001b[0m\u001b[32m'agent_scratchpad'\u001b[0m, \u001b[32m'chat_history'\u001b[0m, \u001b[32m'input'\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33minput_types\u001b[0m=\u001b[1m{\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[32m'chat_history'\u001b[0m: list\u001b[1m[\u001b[0mtyping.Annotated\u001b[1m[\u001b[0mtyping.Union\u001b[1m[\u001b[0mtyping.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.ai.AIMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'ai'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.human.HumanMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'human'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.chat.ChatMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'chat'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.system.SystemMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'system'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.function.FunctionMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'function'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.tool.ToolMessage, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'tool'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.ai.AIMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'AIMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.human.HumanMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'HumanMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.chat.ChatMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'ChatMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.system.SystemMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'SystemMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.function.FunctionMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'FunctionMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m, typing.Annotated\u001b[1m[\u001b[0mlangchain_core.messages.tool.ToolMessageChunk, \u001b[1;35mTag\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtag\u001b[0m=\u001b[32m'ToolMessageChunk'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m, \u001b[1;35mFieldInfo\u001b[0m\u001b[1m(\u001b[0m\u001b[33mannotation\u001b[0m=\u001b[35mNoneType\u001b[0m, \u001b[33mrequired\u001b[0m=\u001b[3;92mTrue\u001b[0m, \u001b[33mdiscriminator\u001b[0m=\u001b[1;35mDiscriminator\u001b[0m\u001b[1m(\u001b[0m\u001b[33mdiscriminator\u001b[0m=\u001b[1m<\u001b[0m\u001b[1;95mfunction\u001b[0m\u001b[39m _get_type at \u001b[0m\u001b[1;36m0x7e42c61ae700\u001b[0m\u001b[39m>, \u001b[0m\u001b[33mcustom_error_type\u001b[0m\u001b[39m=\u001b[0m\u001b[3;35mNone\u001b[0m\u001b[39m, \u001b[0m\u001b[33mcustom_error_message\u001b[0m\u001b[39m=\u001b[0m\u001b[3;35mNone\u001b[0m\u001b[39m, \u001b[0m\u001b[33mcustom_error_context\u001b[0m\u001b[39m=\u001b[0m\u001b[3;35mNone\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m,\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[32m'agent_scratchpad'\u001b[0m\u001b[39m: list\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mtyping.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mtyping.Union\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mtyping.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.ai.AIMessage, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'ai'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.human.HumanMessage, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'human'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.chat.ChatMessage, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'chat'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.system.SystemMessage, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'system'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.function.FunctionMessage, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'function'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.tool.ToolMessage, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'tool'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.ai.AIMessageChunk, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'AIMessageChunk'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.human.HumanMessageChunk, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'HumanMessageChunk'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.chat.ChatMessageChunk, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'ChatMessageChunk'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.system.SystemMessageChunk, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'SystemMessageChunk'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.function.FunctionMessageChunk, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'FunctionMessageChunk'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, typing.Annotated\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39mlangchain_core.messages.tool.ToolMessageChunk, \u001b[0m\u001b[1;35mTag\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mtag\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'ToolMessageChunk'\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m]\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m, \u001b[0m\u001b[1;35mFieldInfo\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mannotation\u001b[0m\u001b[39m=\u001b[0m\u001b[35mNoneType\u001b[0m\u001b[39m, \u001b[0m\u001b[33mrequired\u001b[0m\u001b[39m=\u001b[0m\u001b[3;92mTrue\u001b[0m\u001b[39m, \u001b[0m\u001b[33mdiscriminator\u001b[0m\u001b[39m=\u001b[0m\u001b[1;35mDiscriminator\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mdiscriminator\u001b[0m\u001b[39m=<function _get_type at \u001b[0m\u001b[1;36m0x7e42c61ae700\u001b[0m\u001b[1m>\u001b[0m, \u001b[33mcustom_error_type\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[33mcustom_error_message\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[33mcustom_error_context\u001b[0m=\u001b[3;35mNone\u001b[0m\u001b[1m)\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\n",
       "\u001b[2;32m│   \u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33mpartial_variables\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   \u001b[0m\u001b[33mmessages\u001b[0m=\u001b[1m[\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mSystemMessagePromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33mprompt\u001b[0m=\u001b[1;35mPromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_variables\u001b[0m=\u001b[1m[\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_types\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mpartial_variables\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mtemplate\u001b[0m=\u001b[32m'You are very powerful assistant, but bad at calculating lengths of words.'\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33madditional_kwargs\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mMessagesPlaceholder\u001b[0m\u001b[1m(\u001b[0m\u001b[33mvariable_name\u001b[0m=\u001b[32m'chat_history'\u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mHumanMessagePromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33mprompt\u001b[0m=\u001b[1;35mPromptTemplate\u001b[0m\u001b[1m(\u001b[0m\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_variables\u001b[0m=\u001b[1m[\u001b[0m\u001b[32m'input'\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33minput_types\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mpartial_variables\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
       "\u001b[2;32m│   │   │   │   \u001b[0m\u001b[33mtemplate\u001b[0m=\u001b[32m'\u001b[0m\u001b[32m{\u001b[0m\u001b[32minput\u001b[0m\u001b[32m}\u001b[0m\u001b[32m'\u001b[0m\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   │   \u001b[0m\u001b[33madditional_kwargs\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1m)\u001b[0m,\n",
       "\u001b[2;32m│   │   \u001b[0m\u001b[1;35mMessagesPlaceholder\u001b[0m\u001b[1m(\u001b[0m\u001b[33mvariable_name\u001b[0m=\u001b[32m'agent_scratchpad'\u001b[0m\u001b[1m)\u001b[0m\n",
       "\u001b[2;32m│   \u001b[0m\u001b[1m]\u001b[0m\n",
       "\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pprint(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9b7ebd9c-b46c-4745-b087-1a4952e77261",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:11:08.782432Z",
     "iopub.status.busy": "2024-09-29T14:11:08.781800Z",
     "iopub.status.idle": "2024-09-29T14:11:08.790770Z",
     "shell.execute_reply": "2024-09-29T14:11:08.788458Z",
     "shell.execute_reply.started": "2024-09-29T14:11:08.782380Z"
    }
   },
   "outputs": [],
   "source": [
    "from langchain_core.messages import AIMessage, HumanMessage\n",
    "\n",
    "chat_history = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1a0276b2-867a-45bf-ac0d-ad7badf82b52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:11:22.548448Z",
     "iopub.status.busy": "2024-09-29T14:11:22.547802Z",
     "iopub.status.idle": "2024-09-29T14:11:22.560173Z",
     "shell.execute_reply": "2024-09-29T14:11:22.558461Z",
     "shell.execute_reply.started": "2024-09-29T14:11:22.548396Z"
    }
   },
   "outputs": [],
   "source": [
    "agent = (\n",
    "    {\n",
    "        \"input\": lambda x: x[\"input\"],\n",
    "        \"agent_scratchpad\": lambda x: format_to_openai_tool_messages(\n",
    "            x[\"intermediate_steps\"]\n",
    "        ),\n",
    "        \"chat_history\": lambda x: x[\"chat_history\"],\n",
    "    }\n",
    "    | prompt\n",
    "    | llm_with_tools\n",
    "    | OpenAIToolsAgentOutputParser()\n",
    ")\n",
    "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4caa4959-ba0e-44dd-97a0-67d552c113db",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:11:50.033589Z",
     "iopub.status.busy": "2024-09-29T14:11:50.032955Z",
     "iopub.status.idle": "2024-09-29T14:11:51.988311Z",
     "shell.execute_reply": "2024-09-29T14:11:51.986341Z",
     "shell.execute_reply.started": "2024-09-29T14:11:50.033537Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `get_word_length` with `{'word': 'ddadgawedaewu'}`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m13\u001b[0m\u001b[32;1m\u001b[1;3mThe word \"ddadgawedaewu\" has 13 letters.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "input1 = \"how many letters in the word ddadgawedaewu?\"\n",
    "result = agent_executor.invoke({\"input\": input1, \"chat_history\": chat_history})\n",
    "chat_history.extend(\n",
    "    [\n",
    "        HumanMessage(content=input1),\n",
    "        AIMessage(content=result[\"output\"]),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dd7c40ac-4be5-4414-ad5b-a8e1318b1764",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:12:14.297027Z",
     "iopub.status.busy": "2024-09-29T14:12:14.296178Z",
     "iopub.status.idle": "2024-09-29T14:12:14.307156Z",
     "shell.execute_reply": "2024-09-29T14:12:14.305299Z",
     "shell.execute_reply.started": "2024-09-29T14:12:14.296994Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'how many letters in the word ddadgawedaewu?',\n",
       " 'chat_history': [HumanMessage(content='how many letters in the word ddadgawedaewu?', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='The word \"ddadgawedaewu\" has 13 letters.', additional_kwargs={}, response_metadata={})],\n",
       " 'output': 'The word \"ddadgawedaewu\" has 13 letters.'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "af5826de-dca2-4c58-ae35-1c0697ac46e3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:11:58.183815Z",
     "iopub.status.busy": "2024-09-29T14:11:58.183181Z",
     "iopub.status.idle": "2024-09-29T14:11:58.195362Z",
     "shell.execute_reply": "2024-09-29T14:11:58.193114Z",
     "shell.execute_reply.started": "2024-09-29T14:11:58.183762Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='how many letters in the word ddadgawedaewu?', additional_kwargs={}, response_metadata={}),\n",
       " AIMessage(content='The word \"ddadgawedaewu\" has 13 letters.', additional_kwargs={}, response_metadata={})]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4ebfb657-90c7-4907-9a81-9bac8ba4fc31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-29T14:12:55.930101Z",
     "iopub.status.busy": "2024-09-29T14:12:55.929482Z",
     "iopub.status.idle": "2024-09-29T14:12:57.612696Z",
     "shell.execute_reply": "2024-09-29T14:12:57.610746Z",
     "shell.execute_reply.started": "2024-09-29T14:12:55.930050Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mNo, \"ddadgawedaewu\" is not a real word. It appears to be a random combination of letters.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'is that a real word?',\n",
       " 'chat_history': [HumanMessage(content='how many letters in the word ddadgawedaewu?', additional_kwargs={}, response_metadata={}),\n",
       "  AIMessage(content='The word \"ddadgawedaewu\" has 13 letters.', additional_kwargs={}, response_metadata={})],\n",
       " 'output': 'No, \"ddadgawedaewu\" is not a real word. It appears to be a random combination of letters.'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke({\"input\": \"is that a real word?\", \"chat_history\": chat_history})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61d1eccc-b04f-453a-b943-a9e8494f7324",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "agent",
   "language": "python",
   "name": "agent"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
